package com.netease.xone.app;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;

import msgCenter.MsgCenter;
import preferMgr.XonePrefHelper;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;
import android.os.Handler;
import app.BaseApplication;

import com.netease.cache.CacheManager;
import com.netease.image.ImageManager;
import com.netease.log.NTLog;
import com.netease.mobidroid.DATracker;
import com.netease.pkgRelated.XoneConsts;
import com.netease.push.PushSetting;
import com.netease.service.BaseService;
import com.netease.util.AndroidUtil;
import com.netease.util.RSA;
import com.netease.xone.activity.ActivityLogin;

import db.manager.ManagerAccount;

public class XoneApp extends BaseApplication {
	public static final String TAG = "XoneApp";
	private static XoneApp sApp;
	public static final int PUSH_DELAY_TIME = 1000*5;//启动后开启push的延迟时间

	static public XoneApp getAppInstance() {
		if (sApp == null)
			throw new NullPointerException("sApp not create or be terminated!");
		return sApp;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		sApp = this;
		BaseService.initServiceContext(this);
		initStatic();
		String packetName = getPackageName();
		NTLog.init(packetName);
		NTLog.i(TAG, " onCreate");
	}
	private void initStatic(){
	    SharedKeyMgr.initSharedKey();
	    CacheManager.FileCacheRoot = XoneConsts.CACHE_CHILD_DIR;
	    PushSetting.setProductId(XoneConsts.PUSH_PRODUCT_ID);
	    RSA.RsaCreateInstance();
	    MsgCenter.getInstance(this);
	    boolean readModeText = XonePrefHelper.getReadModeText(this);
	    if(readModeText){
	        ImageManager.DownloadType = ImageManager.NO_DOWNLOAD;
	    }else {
	        ImageManager.DownloadType = ImageManager.ALL_DOWNLOAD;
	    }
	    new Handler().postDelayed(mMyRunnable, PUSH_DELAY_TIME);
	    
	}
	 //开启push
    private Runnable mMyRunnable = new Runnable() {
        @Override
        public void run() {
            if (XonePrefHelper.getNeedPush(getApplicationContext())) {
                MsgCenter.getInstance(getApplicationContext()).launchService();
            }
       } 
    };
	@Override
	public void onExceptionExit(Throwable exception) {
		if (exception != null) {
			StringWriter sw = new StringWriter();
			exception.printStackTrace(new PrintWriter(sw));
			NTLog.d(TAG, "onExceptionExit " + sw.toString());
			if (exception instanceof Exception) {
				DATracker.getInstance().trackExceptionWithCallstack(
						(Exception) exception);
			}

		}
		// Process.killProcess(Process.myPid());
	}

	@Override
	public File getCacheDir() {
		File file = null;
		if (AndroidUtil.hasStorage()) {
			ApplicationInfo info = null;
			String label = null;
			try {
				info = getPackageManager().getApplicationInfo(getPackageName(),
						PackageManager.GET_META_DATA);
				label = getPackageManager().getApplicationLabel(info)
						.toString();
			} catch (NameNotFoundException e) {
				e.printStackTrace();
			}

			file = new File(Environment.getExternalStorageDirectory()
					+ File.separator + label.toLowerCase() + File.separator);
			return file;
		}

		return super.getCacheDir();
	}
	/**
	 * 每个需要登录的请求发起前调用此方法判断，返回true才发起请求,返回false时该方法会自动弹登录界面
	 * @return
	 */
	public boolean isLoginUser(Context context) {
		boolean res =  ManagerAccount.getInstance().isNormalLoginUser();
		if(!res) {
			ActivityLogin.launch(context,false);
		}
		return res;
	}
}
